import java.awt.Color;
import java.io.IOException;
import java.io.Serializable;
import java.util.Hashtable;

import javax.swing.SwingUtilities;

import mobilefeel.AndroidDevice;
import mobilefeel.DeviceListener;
import mobilefeel.MobileFeel;
import mobilefeel.dto.TouchEvent;
import mobilefeel.touchscreen.OnTouchListener;
import mobilefeel.touchscreen.View;
import mobilefeel.touchscreen.ViewListener;



public class PizarraMultitouch implements DeviceListener, ViewListener, OnTouchListener {

	private Hashtable<AndroidDevice, VistaPizarra> pizarras;
	
	public PizarraMultitouch(){
		pizarras = new Hashtable<AndroidDevice, VistaPizarra>();
		
		MobileFeel androidSDK = new MobileFeel();
		androidSDK.addDeviceListener(this);
		try {
			androidSDK.listen(51460);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) {
		new PizarraMultitouch();
	}


	@Override
	public void onDeviceConnected(AndroidDevice device) {
		VistaPizarra pizarra = new VistaPizarra();
		pizarras.put(device, pizarra);
		
		device.getTouchScreen().addViewListener(this);
	}


	@Override
	public void onDeviceDisconnected(AndroidDevice device) {
		pizarras.get(device).dispose();
	}


	@Override
	public void onViewRegistered(AndroidDevice device, View view) {
		try {
			if (view.getName().equals("Pizarra"))
				view.addOnTouchListener(this);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	@Override
	public void onTouch(final View v, final TouchEvent e) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				System.out.println("Touch Action: " + e.getAction() + " en " + v.getName());
				System.out.println("Action Pointer: " + e.getActionPointer());
				System.out.println("Current: ");
				for(int i=0 ; i<e.getPointerCount(); i++ )
					System.out.println("PointerIndex: "+ i + " PointerId: " + e.getPointerId(i) + " X: " + e.getCurrentByIndex(i).getX() + " Y: " + e.getCurrentByIndex(i).getY());
				System.out.println("Event Time: " + e.getEventTime());
				System.out.println("Historical: ");
				for (int j = 0; j<e.getHistorySize(); j++) {
					for(int i=0 ; i<e.getPointerCount(); i++ )
						System.out.println("HPos: " + j + " PointerIndex: "+ i + " PointerId: " + e.getPointerId(i) + " X: " + e.getHistoricalByIndex(i, j).getX() + " Y: " + e.getHistoricalByIndex(i, j).getY());
					System.out.println("Historical Event Time HPos " + j + ": " + e.getHistoricalEventTime(j));
				}
				System.out.println("**************************************");
				
				VistaPizarra pizarra = pizarras.get(v.getAndroidDevice());
				switch(e.getAction()){
				case TouchEvent.ACTION_DOWN:
					Linea linea = new Linea(e.getPointerId(0));
					linea.addPunto(e.getCurrentByIndex(0));
					pizarra.nuevaLinea(linea);
					pizarra.repaint();
					break;
				
				case TouchEvent.ACTION_POINTER_DOWN:
					Linea lineaPointerDown = new Linea(e.getActionPointer());
					Color color = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255));
					lineaPointerDown.setColor(color);
					lineaPointerDown.addPunto(e.getCurrentById(e.getActionPointer()));
					pizarra.nuevaLinea(lineaPointerDown);
					
					for(int pointer = 0; pointer < e.getPointerCount(); pointer++){
						Linea lineaMove = pizarra.getUltimaLinea(e.getPointerId(pointer));
						if (lineaMove != null && e.getPointerId(pointer) != e.getActionPointer()) {
							lineaMove.addPunto(e.getCurrentById(lineaMove.getPointer()));
						}
					}
					pizarra.repaint();
					break;
					
				case TouchEvent.ACTION_MOVE:
					for(int pointer = 0; pointer < e.getPointerCount(); pointer++){
						Linea lineaMove = pizarra.getUltimaLinea(e.getPointerId(pointer));
						if (lineaMove != null) {
							for(int i = 0; i < e.getHistorySize(); i++)
								lineaMove.addPunto(e.getHistoricalById(lineaMove.getPointer(), i));			
							lineaMove.addPunto(e.getCurrentById(lineaMove.getPointer()));
							}
					}
					pizarra.repaint();
					break;
				}
		
			}
		});
	}


	@Override
	public void onCustomDataReceived(AndroidDevice device, String text, Serializable object) {
		if(text.equals("menu") && ((String)object).equals("borrar"))
			pizarras.get(device).borrar();
		
	}

}
